Detect and warn about mutual recursion with #[kani::recursion]#4580
Open
feliperodri wants to merge 1 commit intomodel-checking:mainfrom
Open
Detect and warn about mutual recursion with #[kani::recursion]#4580feliperodri wants to merge 1 commit intomodel-checking:mainfrom
#[kani::recursion]#4580feliperodri wants to merge 1 commit intomodel-checking:mainfrom
Conversation
8ee1f22 to
1c1e669
Compare
…recursion] The per-function REENTRY mechanism used by #[kani::recursion] only handles direct recursion (f calls f) soundly. For mutual recursion (f calls g, g calls f), the REENTRY flag for g is never set, so g's body executes fully instead of being replaced by its contract. This is a silent soundness gap — no error or warning was emitted. This change adds check_mutual_recursion() in the contract transform pass. When a function with #[kani::recursion] is being processed in RecursiveCheck mode, we scan its MIR body for calls to other functions that also have contracts AND #[kani::recursion]. For each such callee, we check if the callee's body calls back to the original function. If so, we emit a span_warn pointing at the call site. We require both has_contract() and has_recursion() on the callee because if the callee has a contract but no #[kani::recursion], Kani replaces the call with the contract abstraction — no mutual recursion occurs. Limitations: - Only detects one level of indirection (f->g->f), not deeper chains. - Reports only the first mutual-recursive callee per function. Includes a test case (mutual_recursion_unsound.rs) with two mutually recursive functions that triggers the warning.
1c1e669 to
5b1c860
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Emit a compile-time warning when a function with
#[kani::recursion]is involved in mutual recursion. The per-functionREENTRYmechanism only handles direct recursion soundly and mutual recursion silently produces unsound results with no diagnostic.Contributes to #3316, #3273.
Problem
The
#[kani::recursion]attribute uses a per-function REENTRY flag to detect when a function calls itself, replacing the recursive call with the function's contract. For mutual recursion (fcallsg,gcallsf), the REENTRY flag forgis never set when verifyingf's contract, sog's body executes fully instead of being replaced by its contract. This is a silent soundness gap — verification succeeds but the result is meaningless.Solution
check_mutual_recursion()incontracts.rs: When a function with#[kani::recursion]is being processed inRecursiveCheckmode, scans its MIR body for calls to other functions that have contracts. For each such callee, checks if the callee's body calls back to the original function (one level of indirection). If so, emits aspan_warnat the call site.Example warning
Testing
mutual_recursion_unsound.rs— two mutually recursive functions (mutual_a↔mutual_b) with contracts and#[kani::recursion], verifying the warning is emitted.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.